﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;
using MESCodeFile;
using LiveNotificationSystem;
namespace mes_tublay
{
    public partial class frmTreasuryeBPLS : Form
    {
        //Exclusive to enable the fully threaded mode of the CodeProcessor.
        //Yields faster time on multi-core processors but stalls greatly with single-cores.
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Exclusive);
        CodeScript script;
        
        DataCache dat = new DataCache("MyData");
        //In memory representation of added data
        DataCache feeLst = new DataCache("FEELIST");
        DataCache feeTbl = new DataCache("FEETABLE");
        DataCache gt = new DataCache("GROSS");
        DataCache addbusiness = new DataCache("ADDBUSINESS");
        DataCache taxsubbusiness = new DataCache("BUSINESSSUB");
        DataCache pmt = new DataCache("PERMIT");
        bool tab1isOk = false;
        bool tab2isOk = false;
        bool tab3isOk = false;
        bool updateassessment = false;
        Timer tmr;
        string officesource;
        public string appno, businessref, referenceNumber="";
        AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
        AutoCompleteStringCollection feelist = new AutoCompleteStringCollection();
        int q;
        string[] blist = new string[0];
        string[] flist = new string[0];
        string[] charge = new string[0];
        double assess, eztax, tot;
        public frmTreasuryeBPLS()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
            proc.ProcessCompleted+=new EventHandler(proc_ProcessCompleted);
         
            txtlineofbusiness.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtlineofbusiness.AutoCompleteSource = AutoCompleteSource.CustomSource;

            txtregulatoryfee.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtregulatoryfee.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }

        #region Assess business according to code
        void txtlineofbusiness_LostFocus(object sender, EventArgs e)
        {
            string cd = txtlineofbusiness.Text;
           //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            //Instantiate the script object and specify a name for the script.
            script = new CodeScript("GETCODE");

            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            //Modified the settings a bit, paki balik na lang po.
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));

            //Modified  your query a bit.
            script.Add(CodeIns.DBU_QUERY, "Select  fCode, fDescription from tbllineofbusiness where fDescription='"+cd+"'");

            //All commands are executed here, however the results will be fetched from
            //the ProcessCompleted event. :)

            script.Add(CodeIns.DBU_END);

            //After Contructing the script, pass it to the processor.
            proc.Run(script);
           
        }
        #endregion
        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            //Make use of the SafeCall method ot safely obtain result from the codeprocessor
            //As it is run from a separate thread.
            proc.SafeCall(this, delegate
            {
                //Check for errors, if there are errors, show it and return to calling code.
                if (proc.State == ProcessorFlag.ReadyErr ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.Error)
                {
                    MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //If there are no errors,
                //Check for the Script's name
                switch (proc.GetLastProcessName())
                {
                      case "POPULATETABLE":
                        //Fetch the data.
                        proc.DataFetch(out dat);
                        //Set the active page to export.
                        dat.SetActivePage(0);
                        //Show the queryu result to the DataGridView
                        if (!txtlineofbusiness.Focused)
                        {
                            txtlineofbusiness.AutoCompleteCustomSource.AddRange(dat.GetStringList(1));
                        }
                        dat.SetActivePage(1);
                        q = dat.GetRecordCount(1);
                        flist = new string[q];
                        flist = dat.GetStringList("item");
                        feelist.AddRange(flist);
                        txtregulatoryfee.AutoCompleteCustomSource = feelist;

                        //Instead of using DataSource, make use of the Items property.
                        lstcharges.Items.Clear();
                        lstcharges.Items.AddRange(dat.GetObjectList("item"));
                        dat.SetActivePage(2);
                        gridfeesandcharges.DataSource = dat.ExportPage();
                        txtregulatoryfee_query(null, null);
                        dat.SetActivePage(3);
                        if (dat.GetValue(0, "total").ToString().Length > 0)
                        {
                            tot = Double.Parse(dat.GetValue(0, "total").ToString());
                        }
                        else
                        {
                            tot = 0.00;
                        }
                        dat.SetActivePage(4);
                       txtlineofbusiness.Text = dat.GetValue(0, "busyness").ToString();
                       getinitialcode();
                      break;
                    //Load the Fee to listbox
                    case "LOADFEE":
                        proc.DataFetch(out feeTbl);
                        feeTbl.SetActivePage(0);
                        lstcharges.Items.Clear();
                        lstcharges.Items.AddRange(feeTbl.GetObjectList("item"));
                        break;

                    case "GETCODE":
                        proc.DataFetch(out dat);
                        dat.SetActivePage(0);
                        if (dat.GetRecordCount(0) > 0)
                        {
                            string cde;
                            cde = dat.GetValue(0, "fCode").ToString();
                            lblcode.Text = cde;
                            cde = lblcode.Text;
                            
                        }
                        break;
                    case "GROSSTAX":
                        {
                            proc.DataFetch(out gt);
                            gt.SetActivePage(0);
                            //Nonessential gross income tax computation
                            int numresult = gt.GetRecordCount(0);
                            if (numresult > 0)
                            {
                                string g = gt.GetValue(0, "annumtax").ToString();
                                string f;
                                f = "#,###.00";
                                double grr, adjustgrr, mod, ez;
                                grr = Double.Parse(g);
                                ez = Double.Parse(txtgrossnonessential.Value.ToString());
                                string revcode;
                                revcode = lblcode.Text;
                                switch (revcode)
                                {
                                    case "5.8.a":
                                        {
                                            if (ez > 6499999.99)
                                            {
                                                mod = ez - 6500000;
                                            }
                                            else
                                            {
                                                mod = 0;
                                            }
                                            adjustgrr = grr + (mod * 0.00375) + eztax;
                                            lblgrosssalestax.Text = adjustgrr.ToString(f);
                                            break;
                                        }
                                    case "5.8.b":
                                        {
                                            if (ez > 1999999.99)
                                            {
                                                mod = ez - 2000000;
                                            }
                                            else
                                            {
                                                mod = 0;
                                            }
                                            adjustgrr = grr + (mod * 0.0005) + eztax;
                                            lblgrosssalestax.Text = adjustgrr.ToString(f);
                                            break;
                                        }
                                    case "5.8.e":
                                        {
                                            if (ez > 1999999.99)
                                            {
                                                mod = ez - 2000000;
                                            }
                                            else
                                            {
                                                mod = 0;
                                            }
                                            adjustgrr = grr + (mod * 0.0005) + eztax;
                                            lblgrosssalestax.Text = adjustgrr.ToString(f);
                                            break;
                                        }
                                }
                            }
                            /*
                            if (chknewbusiness.Checked == true)
                            {
                                adjustgrr = 0.00;
                                lblgrosssalestax.Text = adjustgrr.ToString(f);
                            }
                             * */
                            break;
                        }
                    case "ESSENTIAL":
                        proc.DataFetch(out gt);
                        gt.SetActivePage(0);
                        //Essential gross income tax computation
                        double ez1, ez2, ez3, ezez;
                        string ezentiala = gt.GetValue(0, "annumtax").ToString();
                        ez1 = Double.Parse(ezentiala);
                        gt.SetActivePage(1);
                        string ezentialb = gt.GetValue(0, "annumtax").ToString();
                        ez2 = Double.Parse(ezentialb);
                        //ezez = Double.Parse(txtgrossessential.Value.ToString());
                        ezez = Double.Parse(txtgrossnonessential.Value.ToString());
                        if (ezez <= 400000)
                        {
                            ez3 = ezez * 0.02;
                        }
                        else
                        {
                            ez3 = ezez * 0.01;
                        }
                        double ezgrosstax;
                        if (ez1 > ez2)
                        {
                            ezgrosstax = ez1;
                        }
                        else
                        {
                            ezgrosstax = ez2;
                        }
                        if (ez3 > ezgrosstax)
                        {
                            ezgrosstax = ez3;
                        }
                        gt.SetActivePage(2);
                        string nonezentil = gt.GetValue(0, "annumtax").ToString();
                        double nonez = Double.Parse(nonezentil);
                        eztax = ezgrosstax / 2;
                        double totax, nonezgross;
                        nonezgross = Double.Parse(txtgrossnonessential.Value.ToString());
                        if (nonezgross != 0)
                        {
                            totax = eztax + nonez;
                        }
                        else
                        {
                            totax = eztax;
                        }
                        if (chknewbusiness.Checked == true)
                        {
                            totax = 0.00;
                        }
                        string fmt = "#,###.00";
                        lblgrosssalestax.Text = totax.ToString(fmt);
                        nonezential();
                        break;

                    case "LOADBUSINESS":
                        proc.DataFetch(out addbusiness);
                        addbusiness.SetActivePage(0);
                        gridbusinesses.DataSource = addbusiness.ExportPage();
                        addbusiness.SetActivePage(1);
                        lblsubtax.Text = addbusiness.GetValue(0, "totalassess").ToString();
                        break;
                    case "DELETEBUSINESS":
                        proc.DataFetch(out addbusiness);
                        addbusiness.SetActivePage(0);
                        gridbusinesses.DataSource = addbusiness.ExportPage();
                        addbusiness.SetActivePage(1);
                        string fm = "##,###.00";
                        double bustotal;
                        bustotal = Double.Parse(addbusiness.GetValue(0, "total").ToString());
                        lblsubtax.Text = bustotal.ToString(fm);
                        break;
                    case "SAVEASSESSMENT":
                        proc.DataFetch(out taxsubbusiness);
                        taxsubbusiness.SetActivePage(0);
                        gridbusinesses.DataSource = taxsubbusiness.ExportPage();
                        taxsubbusiness.SetActivePage(1);
                        double businesstotal;
                        string formt = "##,###.00";
                        businesstotal = Double.Parse(taxsubbusiness.GetValue(0, "subtaxbusiness").ToString());
                        lblsubtax.Text = businesstotal.ToString(formt);
                        break;
              }
            });
        }
        private void InitializeForm()
        {
            frmTreasuryeBPLS_Load();
            chknewbusiness.Click += new EventHandler(chknewbusiness_Click);
            btnnxt1.Click += new EventHandler(btnnxt1_Click);
            txtlineofbusiness.LostFocus += new EventHandler(txtlineofbusiness_LostFocus);
            txtlineofbusiness.TextChanged += new EventHandler(txtlineofbusiness_TextChanged);
            btnaddtax.Click += new EventHandler(btnaddtax_Click);
            txtregulatoryfee.KeyUp += new KeyEventHandler(txtregulatoryfee_query);
            txtcapitalization.ValueChanged += new EventHandler(txtcapitalization_ValueChanged);
            tabControl1.Click += new EventHandler(tabControl1_Click);
            btndelete.Click += new EventHandler(btndelete_Click);
            btnaddbusiness.Click += new EventHandler(btnaddbusiness_Click);
            //Construct the FeeLst DataCache here, no need to contruct FeeTbl as it is used to store the result of a query.
            feeLst = new DataCache("FEELIST");
            feeLst.CreatePage("FEETABLE", new string[] { "ITEM", "REFERENCE", "FEE" }, new Type[] { typeof(string), typeof(string), typeof(float) });
            feeLst.SetMetaData("In memory data");
            feeLst.SetActivePage(0);
         
            btnsave.Click += new EventHandler(btnsave_Click);
            btndeletebusiness.Click += new EventHandler(btndeletebusiness_Click);
            btnapprove.Click += new EventHandler(btnapprove_Click);
            txtgrossnonessential.ValueChanged += new EventHandler(txtgrossnonessential_ValueChanged);
            btndisapprove.Click += new EventHandler(btndisapprove_Click);
            btnremarks.Click += new EventHandler(btnremarks_Click);
            btnClose.Click += new EventHandler(delegate
                {
                    Common.NOTIFY.Push("TREASURY_DEPT_ASSESSMENT", "New Business Permit Application: " + appno, new string[] { "" + appno, referenceNumber, businessref, "BPLO" });
                });
        }

        void btnremarks_Click(object sender, EventArgs e)
        {
            tmr.Enabled = false;
            btnremarks.BackColor = Color.Transparent;
            frmRemarksdisplay frm = new frmRemarksdisplay(this);
            frm.ShowDialog();
        }

        void btndisapprove_Click(object sender, EventArgs e)
        {
            frmRemarks frm = new frmRemarks(this);
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                btnClose_Click(null, null);
            }
        }

        void txtgrossnonessential_ValueChanged(object sender, EventArgs e)
        {
            nonezential();
        }

        void btnapprove_Click(object sender, EventArgs e)
        {
            Common.NOTIFY.Push("TREASURY_DEPT_PAYMENT", "New Business Permit Application: " + lblApplicationNumber.Text + "- For Payment of Assessment", new string[] { "" + lblApplicationNumber.Text, lblfullname.Text, lbltotalassessment.Text});
            btnClose_Click(null, null);
        }

        void btndeletebusiness_Click(object sender, EventArgs e)
        {
            string applino = lblApplicationNumber.Text;
            int gbindex = gridbusinesses.CurrentRow.Index;
            //DataGridViewRow dgvrb = gridbusinesses.Rows[gbindex];
            //gridbusinesses.Rows.Remove(dgvrb);
            string pk = gridbusinesses.Rows[gbindex].Cells[0].Value.ToString();
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            //Instantiate the script object and specify a name for the script.
            script = new CodeScript("DELETEBUSINESS");

            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            script.Add(CodeIns.DBU_DELETE, "tblbusinessassessment", "fID='"+pk+"'");
            script.Add(CodeIns.DBU_QUERY, "Select  fID as ENTRY_NUMBER, fCode as Code, fLineOfBusiness as LINE_OF_BUSINESS, fCapitalizationTaxDue as CAPITALIZATION_TAX_DUE, fGrossSalesTax as GROSS_SALES_TAX, fTotalAssessment as TOTAL_ASSESSMENT from tblbusinessassessment where fApplicationNumber='" + applino + "'");
            script.Add(CodeIns.DBU_QUERY, "Select  SUM(fTotalAssessment) as total from tblbusinessassessment where fApplicationNumber='" + applino + "'");
            script.Add(CodeIns.DBU_END);

            //After Contructing the script, pass it to the processor.
            proc.Run(script);
            
        }

        void btnaddbusiness_Click(object sender, EventArgs e)
        {
            updateassessment = false;
            tabControl1.SelectedIndex = 0;
            lblcode.Text = "Code";
            txtlineofbusiness.Text = "";
            txtnumberofunits.Text = "0";
            txtgrossnonessential.Value = 0;
            txtcapitalization.Value = 0;
            lbltaxcapitalization.Text = "0.00";
            gridmayorpermitfee.DataSource = null;
            gridmayorpermitfee.Rows.Clear();
            
        }

        void savebusiness()
        {
            string apno, fn, dateassess, cowd, linebiz, azzezzor;
            int cy, noofunits;
            double ess, noness, cap, captax, gtax, mpfeesubtotal, totalazzez;
            apno = lblApplicationNumber.Text;
            fn = lblfullname.Text;
            dateassess = txtdateassess.Value.ToString("yyyy-MM-dd");
            cowd = lblcode.Text;
            linebiz = txtlineofbusiness.Text;
            cy = int.Parse(txtcalyr.Text);
            noofunits = int.Parse(txtnumberofunits.Text);
            ess = 0;
            noness = Double.Parse(txtgrossnonessential.Value.ToString());
            cap = Double.Parse(txtcapitalization.Value.ToString());
            captax = Double.Parse(lbltaxcapitalization.Text.ToString());
            gtax = Double.Parse(lblgrosssalestax.Text.ToString());
            mpfeesubtotal = Double.Parse(lblpermitfeetotal.Text.ToString());
            totalazzez = captax + gtax + mpfeesubtotal;
            azzezzor = lbltreasuryassessor.Text;
            DialogResult submit = MessageBox.Show("Are all information complete and correct?", "Confirm Submission", MessageBoxButtons.YesNo);
            if (submit == DialogResult.Yes)
            {
                if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
                script = new CodeScript("SAVEASSESSMENT");
                script.Add(CodeIns.SYS_RESET);
                script.Add(CodeIns.DBU_NEW_CORE);
                script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
                script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
                //CHECK WHETHER TO UPDATE OR INSERT A NEW ENTRY
                if (updateassessment == false)
                {
                    //Instantiate the script object and specify a name for the script.
                    script.Add(CodeIns.DBU_INSERT, "tblbusinessassessment",
                        new string[] { "fApplicationNumber", "fCalendarYear", "fFullName", "fDateAssessed", "fCode",
                        "fLineOfBusiness","fNoOfUnits","fEssential","fNonEssential","fCapitalization",
                        "fCapitalizationTaxDue","fGrossSalesTax","fMayorPermitFeeSubtotal","fTotalAssessment","fTAssessor" },
                        new object[] {"'"+apno+"'","'"+cy+"'","'"+fn+"'","'"+dateassess+"'","'"+cowd+"'","'"+linebiz+"'",
                        "'"+noofunits+"'","'"+ess+"'","'"+noness+"'","'"+cap+"'","'"+captax+"'","'"+gtax+"'","'"+mpfeesubtotal+"'","'"+totalazzez+"'","'"+azzezzor+"'"});

                    //Save contents of dataGrid Mayor Fee to database
                    int rc, indeyx;
                    rc = gridmayorpermitfee.RowCount;
                    string itm, cd1;
                    double fi;
                    for (indeyx = 0; indeyx < rc; indeyx++)
                    {
                        itm = gridmayorpermitfee.Rows[indeyx].Cells[0].Value.ToString();
                        cd1 = gridmayorpermitfee.Rows[indeyx].Cells[1].Value.ToString();
                        fi = Double.Parse(gridmayorpermitfee.Rows[indeyx].Cells[2].Value.ToString());
                        script.Add(CodeIns.DBU_INSERT, "tblmayorpermitfeeassessment", new string[] { "fApplicationNumber", "fItem", "fCode", "fFee", "fDateTrans" }, new object[] { "'" + apno + "'", "'" + itm + "'", "'" + cd1 + "'", fi, "'" + dateassess + "'" });
                    }
                    //End of saving

                    //Save contents of dataGrid Other Department Charges to database
                    /*
                    int rc2, indeyx2;
                    rc2 = gridfeesandcharges.RowCount;
                    string itm2, cd2;
                    double fi2;
                    if (rc2 > 0)
                    {
                        for (indeyx2 = 0; indeyx2 < rc2; indeyx2++)
                        {
                            itm2 = gridfeesandcharges.Rows[indeyx2].Cells[0].Value.ToString();
                            cd2 = gridfeesandcharges.Rows[indeyx2].Cells[1].Value.ToString();
                            fi2 = Double.Parse(gridfeesandcharges.Rows[indeyx2].Cells[2].Value.ToString());
                            script.Add(CodeIns.DBU_INSERT, "tblotherfeesandchargesassessment", new string[] { "fApplicationNumber", "fService", "fReference", "fCharge", "fDateTrans" }, new object[] { "'" + apno + "'", "'" + itm2 + "'", "'" + cd2 + "'", fi2, "'" + dateassess + "'" });
                        }
                    }
                    */
                    //End of saving
                }
                else
                {
                    MessageBox.Show("Update entries");
                    script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
                    //UPDATE TBLBUSINESSASSESSMENT
                    script.Add(CodeIns.DBU_UPDATE, "tblbusinessassessment",
                        new string[] {"fCalendarYear", "fFullName", "fDateAssessed", "fCode",
                        "fLineOfBusiness","fNoOfUnits","fEssential","fNonEssential","fCapitalization",
                        "fCapitalizationTaxDue","fGrossSalesTax","fMayorPermitFeeSubtotal","fTotalAssessment","fTAssessor" },
                        new object[] {"'"+cy+"'","'"+fn+"'","'"+dateassess+"'","'"+cowd+"'","'"+linebiz+"'",
                        "'"+noofunits+"'","'"+ess+"'","'"+noness+"'","'"+cap+"'","'"+captax+"'","'"+gtax+"'","'"+mpfeesubtotal+"'",
                        "'"+totalazzez+"'","'"+azzezzor+"'"}, "fAppicationNumber='"+apno+"'");

                    //UPDATE tblmayorpermitfeeassessment
                    //Save contents of dataGrid Mayor Fee to database
                    int rc, indeyx;
                    rc = gridmayorpermitfee.RowCount;
                    string itm, cd1;
                    double fi;
                    for (indeyx = 0; indeyx < rc; indeyx++)
                    {
                        itm = gridmayorpermitfee.Rows[indeyx].Cells[0].Value.ToString();
                        cd1 = gridmayorpermitfee.Rows[indeyx].Cells[1].Value.ToString();
                        fi = Double.Parse(gridmayorpermitfee.Rows[indeyx].Cells[2].Value.ToString());
                        script.Add(CodeIns.DBU_UPDATE, "tblmayorpermitfeeassessment", new string[] { "fApplicationNumber", "fItem", "fCode", "fFee", "fDateTrans" }, new object[] { "'" + apno + "'", "'" + itm + "'", "'" + cd1 + "'", fi, "'" + dateassess + "'" });
                    }
                    //END OF UPDATE
                }
                //END OF CHECKING
                script.Add(CodeIns.DBU_QUERY, "Select fID as Entry_Number, fCode as Code, fLineOfBusiness as LINE_OF_BUSINESS, fCapitalizationTaxDue as CAPITALIZATION_TAX_DUE, fGrossSalesTax as GROSS_SALES_TAX, fTotalAssessment as TOTAL_ASSESSMENT from tblbusinessassessment where fApplicationNumber='" + apno + "'");
                script.Add(CodeIns.DBU_QUERY, "Select SUM(fTotalAssessment) as subtaxbusiness from tblbusinessassessment where fApplicationNumber='" + apno + "'");
                script.Add(CodeIns.DBU_END);
                //After Contructing the script, pass it to the processor.
                proc.Run(script);
                MessageBox.Show("Business Assessment saved", "Saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Business Assessment is not yet saved.", "Save Failed!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        void btnsave_Click(object sender, EventArgs e)
        {
            DialogResult submit = MessageBox.Show("Are all information complete and correct?", "Confirm Submission", MessageBoxButtons.YesNo);
            if (submit == DialogResult.Yes)
            {
                string apno, fn, transdate;
                int cy;
                double sumtotal;
                apno = lblApplicationNumber.Text;
                fn = lblfullname.Text;
                cy = int.Parse(txtcalyr.Text);
                transdate = txtdateassess.Value.ToString("yyyy-MM-dd");
                sumtotal = Double.Parse(lbltotalassessment.Text);
                if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
                //SAVE TOTAL ASSESSMENT TO tblbusinesstotalassessment
                if (updateassessment == false)
                {
                    script = new CodeScript("SAVEBUSINESSES");
                    script.Add(CodeIns.SYS_RESET);
                    script.Add(CodeIns.DBU_NEW_CORE);
                    script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
                    script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
                    script.Add(CodeIns.DBU_INSERT, "tblbusinesstotalassessment",
                        new string[] { "fAppNum", "fFullName", "fCalYr", "fTransDate", "fTotalBusinessesAssessment" },
                        new object[] { "'" + apno + "'", "'" + fn + "'", "'" + cy + "'", "'" + transdate + "'", "'" + sumtotal + "'" });
                    script.Add(CodeIns.DBU_END);
                    proc.Run(script);
                    MessageBox.Show("Business Assessments successfully saved!", "Saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    //UPDATE ASSESSMENT
                    MessageBox.Show("Values had been updated!");
                    //END UPDATE ASSESSMENT
                }
                //Push to Treasurer
                Common.NOTIFY.Push("TREASURY_DEPT_TREASURER", "New Business Permit Application: " + lblApplicationNumber.Text + "- For Approval of Assessment", new string[] { "" + lblApplicationNumber.Text , referenceNumber, "ASSESSMENT"});
                //Close Form
                btnClose_Click(null, null);
            }
            else
            {
                MessageBox.Show("Business Assessment is not yet saved.", "Save Failed!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }

        void txtlineofbusiness_TextChanged(object sender, EventArgs e)
        {
            string biz;
            biz = txtlineofbusiness.Text;
            if (biz.Equals(null) || biz.Equals(""))
            {
                btnnxt1.Enabled = false;
            }
            else
            {
                if (txtgrossnonessential.Value == 0 && lbltaxcapitalization.Text.Equals("0.00"))
                {
                    btnnxt1.Enabled = false;
                }
                else
                {
                    btnnxt1.Enabled = true;
                }
            }

        }
        //REMOVE SELECTED ROW from datagrid
        void btndelete_Click(object sender, EventArgs e)
        {
            int ri;
            ri = gridmayorpermitfee.CurrentRow.Index;
            DataGridViewRow dgvr = gridmayorpermitfee.Rows[ri];
            gridmayorpermitfee.Rows.Remove(dgvr);
            DataGridViewColumn dgvc = gridmayorpermitfee.Columns[2];
            int rc, indeyx;
            double val, feez=0;
            rc = gridmayorpermitfee.RowCount;
            for (indeyx = 0; indeyx < rc; indeyx++)
            {
                val = Double.Parse(gridmayorpermitfee.Rows[indeyx].Cells[2].Value.ToString());
                feez = feez + val;
            }
            lblpermitfeetotal.Text = feez.ToString();
            if (rc == 0)
            {
                btndelete.Enabled = false;
            }
            else
            {
                btndelete.Enabled = true;
            }
        }

        void txtcapitalization_ValueChanged(object sender, EventArgs e)
        {
            double gross, grosstax;
            string format = "#,###.00";
            gross =Double.Parse(txtcapitalization.Value.ToString());
            grosstax = gross * 0.0005;
            lbltaxcapitalization.Text = grosstax.ToString(format);
            btnnxt1.Enabled = true;
        }
        void nonezential()
        {
            double gnonessential;
            string cde = lblcode.Text.ToString();
            gnonessential = Double.Parse(txtgrossnonessential.Value.ToString());
            //grosales = gnonessential + gessential;
            //Check if the processor is executing script, if it is, return to the calling code.
            btnnxt1.Enabled = true;
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
            //Instantiate the script object and specify a name for the script.
            script = new CodeScript("GROSSTAX");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_QUERY, "Select code, gross, annumtax from tblscheduleofvalues where code='" + cde + "' AND gross>=" + gnonessential);
            //All commands are executed here, however the results will be fetched from
            //the ProcessCompleted event. :)
            script.Add(CodeIns.DBU_END);
            //After Contructing the script, pass it to the processor.
            proc.Run(script);
        }
        
        void tabControl1_Click(object sender, EventArgs e)
        {
            int t = tabControl1.SelectedIndex;
            if (t == 3)
            {
                btnnxt1.Enabled = false;
                btnsave.Enabled = true;
            }
            else
            {
                btnnxt1.Enabled = true;
                btnsave.Enabled = false;
            }
            if (t == 0)
                {
                    string cde;
                    double gessential, gnonessential;
                    cde = lblcode.Text;
                    
                    if (!cde.Equals("5.8.c"))
                    {
                        nonezential();
                    }
                    else
                    {
                        gessential = 0;
                        gnonessential = Double.Parse(txtgrossnonessential.Value.ToString());
                        if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
                        //Instantiate the script object and specify a name for the script.
                        script = new CodeScript("ESSENTIAL");
                        script.Add(CodeIns.SYS_RESET);
                        script.Add(CodeIns.DBU_NEW_CORE);
                        script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
                        script.Add(CodeIns.DBU_QUERY, "Select gross, annumtax from tblscheduleofvalues where code='5.8.a' AND gross>=" + gessential);
                        script.Add(CodeIns.DBU_QUERY, "Select gross, annumtax from tblscheduleofvalues where code='5.8.b' AND gross>=" + gessential);
                        script.Add(CodeIns.DBU_QUERY, "Select gross, annumtax from tblscheduleofvalues where code='5.8.a' or code='5.8.b' or code='5.8.d' AND gross>=" + gnonessential);

                        //All commands are executed here, however the results will be fetched from
                        //the ProcessCompleted event. :)
                        script.Add(CodeIns.DBU_END);
                        //After Contructing the script, pass it to the processor.
                        proc.Run(script);
                    }
                }
            
      }
      
        //Done
        void btnaddtax_Click(object sender, EventArgs e)
        {
            //Eto implementation ko sir.
            btnaddtax_sir_tristan();
        }

        //Kay sir tristan, for your reference sir.
        void txtregulatoryfee_query(object sender, KeyEventArgs e)
        {
            //Make sure that everytime that user type something on the txtregulatoryfee,
            //the contents of the listbox change accordingly as well as pre-caching the Fee field.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
            script = new CodeScript("LOADFEE");

            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_QUERY, "Select item as ITEM, reference as REFERENCE, fee as FEE from tblfeecharge where item LIKE '%" + txtregulatoryfee.Text + "%'");

            script.Add(CodeIns.DBU_END);

            proc.Run(script);
        }
        void btnaddtax_sir_tristan()
        {
            //Instead of requery during btnaddtax_Click, load the pre-cached item and fee from
            //feeTbl, as it is much faster.
            //CHECK WHETHER TO ASSESS OR TO UPDATE EXISTING
            int itm = lstcharges.SelectedIndex;
            if (updateassessment == false)
            {
                //CHECK LAST INDEX NUMBER
                //int lastindex = feeLst.GetRecordCount(0) - 1;
                
                    if (itm < 0)
                    {
                        return;
                    }
                    else
                    {
                        feeLst.Insert(feeTbl.GetValue(itm));
                        gridmayorpermitfee.DataSource = feeLst.ExportPage();
                        compmayorfee();
                    }
            }
            else
            {
                //MessageBox.Show("Add entries to existing list");
                //int reccount = feeLst.GetRecordCount(0);
                pmt.SetActivePage(0);
                pmt.Insert(feeTbl.GetValue(itm));
                gridmayorpermitfee.DataSource = pmt.ExportPage();
                compmayorfee();
            }
        }
        void compmayorfee()
        {
            int idx;
            double fix;
            idx = lstcharges.SelectedIndex;
            fix = Double.Parse(feeTbl.GetValue(idx, "fee").ToString());
            assess = assess + fix;
            lblpermitfeetotal.Text = assess.ToString();
            btndelete.Enabled = true;
        }
      void btnnxt1_Click(object sender, EventArgs e)
        {
            enabledisabletabs();
        }
      void enabledisabletabs()
      {
          int y, newy;
          string apnum = lblApplicationNumber.Text;
          y = tabControl1.SelectedIndex;
          newy = y + 1;
          tabControl1.SelectedIndex = newy;
          if (y == 1)
          {
              savebusiness();
              showbusiness();
          }
          if (newy < 3)
          {
              tab1isOk = true;
              ((Control)tabControl1.TabPages[newy]).Enabled = true;
              tab2isOk = true;

              tab3isOk = true;

              btnsave.Enabled = false;
          }
          else
          {
              computetotal();
              btnsave.Enabled = true;
              btnnxt1.Enabled = false;
          }
      }
      void showbusiness()
      {
          string applino = lblApplicationNumber.Text;
          if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
          //Instantiate the script object and specify a name for the script.
          script = new CodeScript("LOADBUSINESS");
          script.Add(CodeIns.SYS_RESET);
          script.Add(CodeIns.DBU_NEW_CORE);
          script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
          script.Add(CodeIns.DBU_QUERY, "Select  fID, fCode as Code, fLineOfBusiness as LINE_OF_BUSINESS, fCapitalizationTaxDue as CAPITALIZATION_TAX_DUE, fGrossSalesTax as GROSS_SALES_TAX, fTotalAssessment as TOTAL_ASSESSMENT from tblbusinessassessment where fApplicationNumber='" + applino + "'");
          script.Add(CodeIns.DBU_QUERY, "Select SUM(fTotalAssessment) as totalassess from tblbusinessassessment where fApplicationNumber='" + applino + "'");
          
          //All commands are executed here, however the results will be fetched from
          //the ProcessCompleted event. :)

          script.Add(CodeIns.DBU_END);

          //After Contructing the script, pass it to the processor.
          proc.Run(script);
      }
      void chknewbusiness_Click(object sender, EventArgs e)
        {
            if (chknewbusiness.Checked == true)
            {
                txtcapitalization.Enabled = true;
                
                txtgrossnonessential.Enabled = false;
                lblgrosssalestax.Text = "0.00";
                
                txtgrossnonessential.Value = 0;
           }
            else
            {
                txtcapitalization.Enabled = false;
                
                txtgrossnonessential.Enabled = true;
                txtcapitalization.Value = 0;
                lbltaxcapitalization.Text = "0.00";
            }
        }

       public void SetAppNumber(string n, string refNum, string brefNum, string office)
        {
            lblApplicationNumber.Text = n;
            referenceNumber = refNum;
            officesource = office;
            businessref = brefNum;
        }
        private void frmTreasuryeBPLS_Load()
        {
           lbltreasuryassessor.Text = CurrentUser.NAME;
            btnremarks.Visible = false;
            checkofficesource();
            loadname();
           
            btnsave.Enabled = false;
            ((Control)tabControl1.TabPages[1]).Enabled  = false;
            ((Control)tabControl1.TabPages[2]).Enabled = false;
            ((Control)tabControl1.TabPages[3]).Enabled = false;
            
            
            txtcapitalization.Enabled = false;
            btndelete.Enabled = false;
            assess = 0;
            btnnxt1.Enabled = false;
            appno = lblApplicationNumber.Text;
            int cyear = int.Parse(DateTime.Now.Year.ToString()) - 1;
            txtcalyr.Text = cyear.ToString();
            txtnumberofunits.Text = "0";
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;
            
            //Instantiate the script object and specify a name for the script.
            script = new CodeScript("POPULATETABLE");

            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            //Modified the settings a bit, paki balik na lang po.
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));

            //Modified  your query a bit.6
            script.Add(CodeIns.DBU_QUERY, "Select  fCode, fDescription from tbllineofbusiness");
            
            script.Add(CodeIns.DBU_QUERY, "Select item as ITEM, fee as FEE from tblfeecharge");

            script.Add(CodeIns.DBU_QUERY, "Select fTypeOfServices as ITEM, fCode as REFERENCE, fFee as CHARGES from tblbusinesspermitfeesandcharges where fApplicationNumber='" + appno + "'");
            script.Add(CodeIns.DBU_QUERY, "Select SUM(fFee) as total from tblbusinesspermitfeesandcharges where fApplicationNumber='" + appno + "'");
            script.Add(CodeIns.DBU_QUERY, "Select fKindOfBusiness as busyness from tblbusinessinformation where fApplicationNumber='" + appno + "'");
             //All commands are executed here, however the results will be fetched from
            //the ProcessCompleted event. :)

            script.Add(CodeIns.DBU_END);

            //After Contructing the script, pass it to the processor.
            proc.Run(script);
           
        }
        
        void loadname()
        {
           CodeScript s = new CodeScript("CONCAT");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));

            s.Add(CodeIns.DBU_QUERY, "Select * from tblbusinessownerprofile  where fReferenceNumber='"+referenceNumber+"'");
            //Check for existing assessments
            s.Add(CodeIns.DBU_END);

            CodeProcessor p = new CodeProcessor(ThreadingMode.Exclusive);
            p.ProcessCompleted += new EventHandler(delegate
            {
                p.SafeCall(this, delegate
                {
                    if (p.State == ProcessorFlag.DataRdyErr ||
                        p.State == ProcessorFlag.Error ||
                        p.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Sampler", p.GetProcessorMessage);
                        return;
                    }
                    DataCache d;
                    p.DataFetch(out d);
                    d.SetActivePage(0);

                    if (d.GetRecordCount(0) > 0)
                    {
                        string ft = d.GetValue(0, "fProfileType").ToString();
                        string businessname, ln, fn, mn;
                        //GET IMAGE
                        if (d.GetValue(0, "fImgName").ToString() != "")
                        {
                            s.Add(CodeIns.SYS_USE_CORE, 2);
                            s.Add(CodeIns.RFU_SET_CONFIG, "ftp://" + Common.CFG.Read("SERVER"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), 256);
                            /*
                            if (isEditorMode)
                                s.Add(CodeIns.RFU_DWLD, Common.LOCPATH + CurrentUser.USERIDNUMBER + Const.USERMANAGERIMG, cache.GetValue(userToEdit, "fImgName").ToString());
                            else
                                s.Add(CodeIns.RFU_DWLD, Common.LOCPATH + Const.USERCURRENT, cache.GetValue(userToEdit, "fImgName").ToString());
                            */
                        }
                        s.Add(CodeIns.RFU_END);
                        if (ft.Equals("CORPORATION"))
                        {
                            businessname = d.GetValue(0, "fCompanyName").ToString();
                        }
                        else
                        {
                            ln = d.GetValue(0, "fLastName").ToString();
                            fn = d.GetValue(0, "fFirstName").ToString();
                            mn = d.GetValue(0, "fMiddleName").ToString();
                            businessname = ln + ", " + fn + " " + mn;
                        }
                        lblfullname.Text = businessname;
                    }
                 });
            });
            p.Run(s);
        }
        void performassessment()
        {
            MessageBox.Show("Assessment will now commence");
        }
        
        void computetotal()
        {
            double perbusinesssubtax = 0;
            double.TryParse(lblsubtax.Text, out perbusinesssubtax);
            double totalassessment = perbusinesssubtax + tot;
            string ft = "##,###.00";
            lbltotalassessment.Text = totalassessment.ToString(ft);
        }
        void getinitialcode()
        {
           CodeScript s = new CodeScript("INITIALCODE");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            string inicode = txtlineofbusiness.Text;
            s.Add(CodeIns.DBU_QUERY, "Select fCode from tbllineofbusiness  where fDescription='"+inicode+"'");
            s.Add(CodeIns.DBU_END);

            CodeProcessor p = new CodeProcessor(ThreadingMode.Exclusive);
            p.ProcessCompleted += new EventHandler(delegate
            {
                p.SafeCall(this, delegate
                {
                    if (p.State == ProcessorFlag.DataRdyErr ||
                        p.State == ProcessorFlag.Error ||
                        p.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Sampler", p.GetProcessorMessage);
                        return;
                    }
                    DataCache c;
                    p.DataFetch(out c);
                    c.SetActivePage(0);
                    lblcode.Text = c.GetValue(0, "fCode").ToString();
                });
            });
            p.Run(s);
        }
         
        void checkofficesource()
        {
            switch (officesource)
            {
                case "BPLO":
                    {
                        btnapprove.Visible = false;
                        btndisapprove.Visible = false;
                    }
                    break;
                case "ASSESSMENT":
                    {
                        btnnxt1.Visible = false;
                        btnapprove.Visible = true;
                        btndisapprove.Visible = true;
                        btnsave.Visible = false;
                    }
                    break;
                case "TREASURER":
                    {
                        btnapprove.Visible = false;
                        btndisapprove.Visible = false;
                        btnnxt1.Visible = true;
                        btnsave.Visible = true;
                        btnremarks.Visible = true;
                        tmr = new Timer();
                        tmr.Interval = 500;
                        tmr.Tick += new EventHandler(delegate
                        {
                            //Cycle through colors
                            {
                                if (btnremarks.BackColor==Color.AliceBlue)
                                     btnremarks.BackColor=Color.DarkSeaGreen; 
                                else
                                    btnremarks.BackColor=Color.AliceBlue;
                            }
                        });
                        tmr.Enabled = true;
                    }
                    break;
            }
            //Check for existing assessment
            CodeScript s = new CodeScript("GETPREVIOUSASSESSMENT");
            s.Add(CodeIns.DBU_NEW_CORE);
            s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            string inicode = txtlineofbusiness.Text;
            appno = lblApplicationNumber.Text;
            s.Add(CodeIns.DBU_QUERY, "Select * from tblbusinessassessment  where fApplicationNumber='" + appno + "'");
            s.Add(CodeIns.DBU_END);

            CodeProcessor p = new CodeProcessor(ThreadingMode.Exclusive);
            p.ProcessCompleted += new EventHandler(delegate
            {
                p.SafeCall(this, delegate
                {
                    if (p.State == ProcessorFlag.DataRdyErr ||
                        p.State == ProcessorFlag.Error ||
                        p.State == ProcessorFlag.ReadyErr)
                    {
                        Confirm.Error("Sampler", p.GetProcessorMessage);
                        return;
                    }
                    DataCache e;
                    p.DataFetch(out e);
                    e.SetActivePage(0);
                    int recres = e.GetRecordCount(0);
                  
                    if (recres > 0)
                    {
                        //DISPLAY ASSESSMENT
                        MessageBox.Show("Assessment had already been performed. Now displaying values");
                        txtcalyr.Text = e.GetValue(0, "fCalendarYear").ToString();
                        lblfullname.Text = e.GetValue(0, "fFullName").ToString();
                        txtdateassess.Text = e.GetValue(0, "fDateAssessed").ToString();
                        txtnumberofunits.Text = e.GetValue(0, "fNoOfUnits").ToString();
                        
                        //VALIDATE WHETHER ESSENTIAL OR NON-ESSENTIAL EXISTS
                        //txtgrossessential.Value = Decimal.Parse(e.GetValue(0, "fEssential").ToString());
                        txtgrossnonessential.Value = Decimal.Parse(e.GetValue(0, "fNonEssential").ToString());
                        
                        
                        txtcapitalization.Value = Decimal.Parse(e.GetValue(0, "fCapitalization").ToString());
                        lbltaxcapitalization.Text = e.GetValue(0, "fCapitalizationTaxDue").ToString();
                        lblgrosssalestax.Text = e.GetValue(0, "fGrossSalesTax").ToString();
                        lblpermitfeetotal.Text = e.GetValue(0, "fMayorPermitFeeSubtotal").ToString();
                        lbltotalassessment.Text = e.GetValue(0, "fTotalAssessment").ToString();
                        lbltreasuryassessor.Text = e.GetValue(0, "fTAssessor").ToString();
                        btnsave.Text = "Update";
                        updateassessment = true;
                        
                        CodeScript cs = new CodeScript("DISPLAYMAYORASSESSMENT");
                        cs.Add(CodeIns.DBU_NEW_CORE);
                        cs.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
                        appno = lblApplicationNumber.Text;
                        cs.Add(CodeIns.DBU_QUERY, "Select fItem as ITEM, fCode as CODE, fFee  as FEE from tblmayorpermitfeeassessment  where fApplicationNumber='" + appno + "'");
                        cs.Add(CodeIns.DBU_QUERY, "Select SUM(fFee) as mayortotal from tblmayorpermitfeeassessment  where fApplicationNumber='" + appno + "'");
                        cs.Add(CodeIns.DBU_QUERY, "Select fDateAssessed as DATE, fCode as Code, fLineOfBusiness as LINE_OF_BUSINESS, fCapitalizationTaxDue as CAPITALIZATION_TAX_DUE, fGrossSalesTax as GROSS_SALES_TAX, fTotalAssessment as TOTAL_ASSESSMENT from tblbusinessassessment where fApplicationNumber='" + appno + "'");
                        cs.Add(CodeIns.DBU_END);

                        CodeProcessor cp = new CodeProcessor(ThreadingMode.Exclusive);
                        cp.ProcessCompleted += new EventHandler(delegate
                        {
                            cp.SafeCall(this, delegate
                            {
                                if (cp.State == ProcessorFlag.DataRdyErr ||
                                    cp.State == ProcessorFlag.Error ||
                                    cp.State == ProcessorFlag.ReadyErr)
                                    {
                                    Confirm.Error("Sampler", p.GetProcessorMessage);
                                    return;
                                    }
                            cp.DataFetch(out pmt);
                            pmt.SetActivePage(0);
                            int rc = pmt.GetRecordCount(0);
                            if (rc > 0)
                            {
                                btndelete.Enabled = true;
                            }
                            else
                            {
                                btndelete.Enabled = false;
                            }
                            gridmayorpermitfee.DataSource = pmt.ExportPage();
                            pmt.SetActivePage(1);
                            lblpermitfeetotal.Text = pmt.GetValue(0, "mayortotal").ToString();
                            assess = Double.Parse(pmt.GetValue(0, "mayortotal").ToString());
                            pmt.SetActivePage(2);
                            gridbusinesses.DataSource = pmt.ExportPage();
                            showbusiness();
                      });
            });
            cp.Run(cs);
            
                    }
                    else
                    {
                        performassessment();
                    }
                });
            });
            p.Run(s);
        }

        }
    }

